/*
 * main.c
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */


#include "main.h"

void main(void)
{  
    sys_init();
    temp_sensor_init();
    uart0_init();
    debug_print("res%x\n", RESETS0);

    slpram_init_handle();
    battery_init();
    heat_init();

    seg_init();
    sbled_init();
    key_init();

#if (CONFIG_MIC_TYPE > 0)  
#if (CONFIG_PLUGE_DETECT == 1)
    if (HEATER_PRE_STAT_READ == true)
        IOPUDET = (IOPUDET & 0xf2) | 0x05;
#endif
#if (CONFIG_POWER_ON_JUDGE_MODE == 0)
    slpram_sleep_handle();
#endif
    if(get_ultralow_power_flag() == false){
        mic_det_init();
    }
#else
    mic_init();
#endif

    heater_plug_detect_init();

    SYS_IRQ_ENABLE();

    while (1) {
        heat_handle_busy_loop();  
              
        if (tick_expired_10) {
#if (CONFIG_MIC_TYPE > 0)
            mic_det_handle_10ms();                
#endif
            mic_handle_10ms();
            vin_detect_handle();
            
            if (!heating_flag) {
                heater_plug_detect_handle_10ms();
                temp_sensor_handle();
            }
            
            lvd_handle_10ms();
            ec_control_handle_10ms();   

            key_handle_10ms();

            ui_handle_10ms();
            sbled_handle_10ms();
            tick_expired_10 = 0;
        }
        
        if (tick_expired_100) {
#if (CONFIG_MIC_TYPE > 0)
            mic_det_handle_100ms();
#endif

            if (!heating_flag) {
                chargerc_handle();
                battery_handle_100ms();
            }

            ec_control_handle_100ms();
            sys_event_handle_100ms();       
            pm_handle_light_load_100ms();

            SYS_WDOG_KEEP_ALIVE();
            tick_expired_100 = 0;
        }

        pm_sleep_handle();    
    }    
}